<?
/*
		topGames: an iRC games bot
		Copyright (C) 2008 Marcos Garc�a <marcosgdf@gmail.com>
    
		This file is part of topGames

		This program is free software: you can redistribute it and/or modify
		it under the terms of the GNU General Public License as published by
		the Free Software Foundation, either version 3 of the License, or
		any later version.

		This program is distributed in the hope that it will be useful,
		but WITHOUT ANY WARRANTY; without even the implied warranty of
		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
		GNU General Public License for more details.

		You should have received a copy of the GNU General Public License
		along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

class raw {
	function action ($tg,$data) {
		$raw = explode(" ",$data);
		$rawNumber = $raw[1];
		if ($rawNumber == 353) {
			$parseNicks = $this->parseNames($data,$raw[4]);
			$this->tres_cinco_tres($tg,$raw[4],str_replace("@","",str_replace("+","",$parseNicks)));
		}
		if ($rawNumber == 439) $this->cuatro_tres_nueve($data);
	}
	
	/**
	 * This function its executed when the bot enters a channel. Then gets the nicks of the people in the channel and stores them into the database
	 * @param $tg
	 * @param $channel
	 * @param $nicks
	 * @return unknown_type
	 */
	function tres_cinco_tres($tg,$channel,$nicks) {
		foreach ($nicks AS $nick) {
			$tg->dbSend("INSERT INTO ".DB_PREFIX."channelusers SET channel='$channel', user='$nick', bot_number='".BOT_NUMBER."'");
		}
	}
	/**
	 * This function its executed when the bot can't enter a channel because of iRC Server's flood protection
	 * @param $tg
	 * @param $data
	 * @return unknown_type
	 */
	function cuatro_tres_nueve($tg,$data) {
		$tg->dbSend("UPDATE ".DB_PREFIX."autojoin SET `is_in`='0' WHERE `channel`='".$data[1]."' AND `bot_number`='".BOT_NUMBER."'");
	}
	function parseNames($names, $canal) {
		$stack="$canal :";
		$names=str_replace($stack, $stack."\n", $names);
		$names=explode("\n", $names);
		$callback=array();
		foreach ($names as $linea){
			$linea=trim($linea);
			if(strlen($linea) < 2): continue; endif;
			if(ereg("^:", $linea)): continue; endif;
			if(ereg('End of', $linea)): continue; endif;
			$callback[]=$linea;
		}
		$nicks=array();
		foreach ($callback as $nick1){
			$line=explode(' ', $nick1);
			foreach($line as $n2){ 
				if(ereg('^#', $n2)): continue; endif;
				if(strlen($n2) < 2): continue; endif;
				$nicks[]=ereg_replace("^:", '', $n2);
			}
		}
		return $nicks;
	}
}
?>